//gp7-[]\;`,.//gp7-[]\;`,.//gp7-[]\;`,.//gp7-[]\;`,.//gp7-[]\;`,.//gp7-[]\;`,.//gp7-[]\;`,.//gp7-[]\;`,.//gp7-[]\;`,.//gp7-[]\;`,.//gp7-[]\;`,.//gp7-[]\;`,.//gp7-[]\;`,.//gp7-[]\;`,.//gp7-[]\;`,.//gp7-[]\;`,.//gp7-[]\;`,.//gp7-[]\;`,.//gp7-[]\;`,./import { PageContainer } from '@ant-design/pro-layout';
import { Button, Divider, message, Modal } from 'antd';
import { useReducer, useRef, useState } from 'react';
import Add from './mainContent/add';
import Update from './mainContent/update';
import { ExclamationCircleFilled, PlusOutlined } from '@ant-design/icons';
import { PageContainer } from '@ant-design/pro-layout';
import { ActionType, ProColumns } from '@ant-design/pro-components';
import { ProTable } from '@ant-design/pro-components';
import styles from './index.less';
import { deleteCharge, getChargeTable } from './service';
import type { chargeTableList } from './types';
import moment from 'moment';
export default () => {
  const actionRef = useRef<ActionType>();
  const [pageName, setPageName] = useState<string>('收费规则列表');
  const [schemeId, setSchemeId] = useState<string>();
  const ElementComponent = {
    AddElement: (
      <Add
        close={() => {
          // eslint-disable-next-line @typescript-eslint/no-use-before-define
          AddClose();
        }}
      />
    ),
    UpdateElement: (
      <Update
        close={() => {
          // eslint-disable-next-line @typescript-eslint/no-use-before-define
          AddClose();
        }}
        id={schemeId}
      />
    ),
  };

  const [componentVisibleText, setComponentVisibleText] = useReducer(
    (state: JSX.Element | string, action: string) => {
      switch (action) {
        case 'list':
          setPageName('收费规则列表');
          return '';
        case 'add':
          setPageName('添加收费规则');
          return ElementComponent.AddElement;
        case 'update':
          setPageName('修改收费规则');
          return ElementComponent.UpdateElement;
      }
      return state;
    },
    '',
  );
  function formatTime(minutes: number) {
    const hours = Math.floor(minutes / 60); // 计算小时数
    const mins = minutes % 60; // 计算分钟数
    const start = moment().startOf('day').add(hours, 'hours').add(mins, 'minutes'); // 创建起始时间
    return start.format('HH:mm');
  }
  const AddClose = () => {
    setComponentVisibleText('list');
  };
  const columns: ProColumns<chargeTableList>[] = [
    {
      title: '规则编号',
      dataIndex: 'id',
      hideInForm: true,
      hideInSearch: false,
    },
    {
      title: '规则名称',
      dataIndex: 'name',
      hideInForm: true,
      hideInSearch: false,
    },
    {
      title: '收费时段',
      dataIndex: 'effectTime',
      hideInSearch: true,
      render: (_, record) => {
        return <>{formatTime(record.effectStartTime) + '-' + formatTime(record.effectEndTime)}</>;
      },
    },
    {
      title: '免费时长(分钟)',
      dataIndex: 'freeTime',
      hideInSearch: true,
    },
    {
      title: '24小时最高收费限额',
      dataIndex: 'maxPrice',
      hideInSearch: true,
      render: (_, record) => {
        return record?.maxPrice ? record?.maxPrice / 100 : '-';
      },
    },
    {
      title: '规则描述',
      dataIndex: 'comment',
      hideInSearch: true,
    },
    {
      title: '创建时间',
      dataIndex: 'createTime',
      hideInSearch: true,
    },

    {
      fixed: 'right',
      title: '操作',
      hideInSearch: true,
      render: (_, record) => {
        return (
          <>
            <a
              onClick={() => {
                setSchemeId(record.id);
                setComponentVisibleText('update');
              }}
            >
              编辑
            </a>
            <Divider type="vertical" />
            <a
              onClick={() => {
                Modal.confirm({
                  title: `确定要删除${record.id}车场吗，?`,
                  icon: <ExclamationCircleFilled />,
                  content: '注意此操作不可逆',
                  okType: 'danger',
                  centered: true,
                  async onOk() {
                    const res = await deleteCharge(record.id);
                    if (res) {
                      message.success('删除成功');
                      actionRef.current?.reload();
                    }
                  },
                });
              }}
            >
              删除
            </a>
          </>
        );
      },
    },
  ];
  return (
    <PageContainer
      title={
        <div>
          {componentVisibleText !== '' && (
            <Button
              // disabled={returnPlay}
              type="link"
              onClick={() => {
                console.log('我被触发了哦');
                setComponentVisibleText('list');
              }}
            >
              <img
                className={styles.toolBarImg}
                src={'/icons/arrow-return-left.svg'}
                alt="Picture of the author"
                width="21"
                height="21"
              />
            </Button>
          )}
          {pageName}
        </div>
      }
    >
      {componentVisibleText === '' && (
        <ProTable<any>
          rowKey="parkinglotId"
          actionRef={actionRef}
          columns={columns}
          scroll={{ x: true }}
          style={{ whiteSpace: 'nowrap' }}
          columnEmptyText="-"
          headerTitle="收费规则列表"
          toolBarRender={() => [
            <Button
              key="button"
              icon={<PlusOutlined />}
              type="primary"
              onClick={() => {
                setComponentVisibleText('add');
              }}
            >
              添加
            </Button>,
          ]}
          pagination={{
            size: 'default',
            showQuickJumper: true,
            showSizeChanger: true,
            defaultPageSize: 10,
          }}
          request={async (param: any) => {
            const res: any = await getChargeTable(param);
            return { ...res, data: res?.data.list };
          }}
        />
      )}
      {componentVisibleText}
    </PageContainer>
  );
};
